﻿using System;
using System.Collections.Generic;
using System.Linq;
using FirstFloor.ModernUI.Windows.Controls;
using IsikFx.Project.CommonTypes;
using IsikFx.Project.CommonTypes.Announcement;
using IsikFx.Project.CommonTypes.User;
using IsikFx.Project.CommonTypes.CRUD;
using IsikFx.Project.CommonTypes.Watchman;
using IsikFx.Project.CommonTypes.FundLog;
using log4net;
using IsikFx.Project.Utils;
namespace IsikFx.Project.ClientApp.Helpers
{
    public static class Helper
    {
        public static readonly string USD = "USD";
        public static readonly string Sales = "Sales";
        public static readonly string Retention = "Retention";
        public static readonly string[] RetentionArray = { "FundIn", "FundOut" };
        public static readonly string[] SalesArray = { "CustomerEntry" };
        public static readonly string GoalBarFormat = ("% ") + "{0}" + ("  ") + "{1} / " + "{2}";
        public static readonly string ExceptionMessage = "Geçici süre işleminizi gerçekleştiremiyoruz, lütfen daha sonra tekrar deneyiniz.";
        public static User _user;
        public static ILog logger = LogManager.GetLogger(typeof(Helper));
        static IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();

        public enum Roles
        {
            Sales = 1,
            Retention = 2,
            Admin = 3
        };

        public static string DomainName()
        {
            return Environment.UserDomainName + "_" + Environment.UserName;
        }

        public static void InitializeCurrentUser()
        {
            UserResponseMessage userResponseMsg = client.GetCurrentUser(Helper.DomainName());
            if (!userResponseMsg.HasError)
            {
                if (userResponseMsg.Response != null && userResponseMsg.Response.User != null && userResponseMsg.Response.User.Count > 0)
                {
                    _user = userResponseMsg.Response.User[0];
                }
            }
            else
            {
                throw new Exception(userResponseMsg.ErrorMessage);
            }
        }

        public static User CurrentUser
        {
            get
            {
                return _user;
            }
        }

        public static List<Customer> GetCustomers()
        {
            try
            {
                List<Customer> customers = client.GetCustomers(Convert.ToInt32(CurrentUser.UserID)).ToList();
                return customers;
            }
            catch (Exception ex)
            {
                ExceptionHelper.HandleException(ex, typeof(Helper));
                return null;
            }
        }

        public static Goal GetAgentGoal()
        {
            IsikFx.Project.CommonTypes.Goal goal = null;
            try
            {
                ModernDialog dialog = new ModernDialog();

                IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
                goal = client.GetAgentGoal(Convert.ToInt32(CurrentUser.UserID));
            }
            catch (Exception ex)
            {
                ExceptionHelper.HandleException(ex, typeof(Helper));
            }
            return goal;
        }

        public static GoalProgress GetGoalProgress()
        {
            GoalProgress goalProgress;
            IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
            goalProgress = client.GetGoalProgress(Convert.ToInt32(CurrentUser.UserID));
            return goalProgress;
        }

        public static AnnouncementResponseMessage GetActiveAnnouncementsAsAdmin(int adminID)
        {
            List<Announcement> announcements = new List<Announcement>();
            IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
            AnnouncementResponseMessage response = client.GetActiveAnnouncementsAsAdmin(adminID);
            return response;
        }

        /// <summary>
        /// GetActiveAnnouncementsAsUser
        /// </summary>
        /// <param name="roleType"></param>
        /// <returns></returns>
        public static AnnouncementResponseMessage GetActiveAnnouncementsAsUser(int roleType)
        {
            AnnouncementResponseMessage responseMsg = new AnnouncementResponseMessage();
            try
            {
                List<Announcement> announcements = new List<Announcement>();
                IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
                responseMsg = client.GetActiveAnnouncementsAsUser(roleType);

            }
            catch (Exception ex)
            {
                ExceptionHelper.HandleException(ex, typeof(Helper));
                responseMsg.HasError = true;
                responseMsg.ErrorMessage = ExceptionMessage;
            }
            return responseMsg;
        }

        /// <summary>
        /// InsertNewAnnouncement
        /// </summary>
        /// <param name="announcementTitle"></param>
        /// <param name="announcement"></param>
        /// <param name="roleType"></param>
        /// <returns></returns>
        public static CrudResponseMessage InsertNewAnnouncement(string announcementTitle, string announcement, int roleType)
        {
            CrudResponseMessage responseMessage = new CrudResponseMessage();
            try
            {
                IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
                string uniqeAnnouncementId = CurrentUser.UserID + "_" + CurrentUser.Name + "_" + CurrentUser.Surname;
                string createDate = DateTime.Now.ToShortDateString();
                int adminID = Convert.ToInt32(CurrentUser.UserID);
                //return 1;
                responseMessage = client.InsertAnnouncement(adminID, uniqeAnnouncementId, announcementTitle, announcement, createDate, roleType);
            }
            catch (Exception ex)
            {
                ExceptionHelper.HandleException(ex, typeof(Helper));
                responseMessage.HasError = true;
                responseMessage.ErrorMessage = ExceptionMessage;
            }

            return responseMessage;
        }


        /// <summary>
        /// ResetGoals
        /// </summary>
        /// <returns></returns>
        public static CrudResponseMessage ResetGoals()
        {
            CrudResponseMessage responseMsg = new CrudResponseMessage();
            try
            {
                IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
                responseMsg = client.ResetGoals();
            }
            catch (Exception ex)
            {
                ExceptionHelper.HandleException(ex, typeof(Helper));
                responseMsg.HasError = true;
                responseMsg.ErrorMessage = ExceptionMessage;
            }
            return responseMsg;
        }


        /// <summary>
        /// DeleteAnnouncement
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static CrudResponseMessage DeleteAnnouncement(int id)
        {
            CrudResponseMessage responseMsg = new CrudResponseMessage();
            try
            {
                IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
                return client.DeleteAnnouncement(id);
            }
            catch (Exception ex)
            {
                ExceptionHelper.HandleException(ex, typeof(Helper));
                responseMsg.HasError = true;
                responseMsg.ErrorMessage = ExceptionMessage;
            }
            return responseMsg;
        }


        /// <summary>
        /// GetWatchmenList
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static WatchmanResponseMessage GetWatchmenList(string date)
        {
            WatchmanResponseMessage responseMsg = new WatchmanResponseMessage();
            try
            {
                IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
                responseMsg = client.GetWatchmenList(date);
            }
            catch (Exception ex)
            {
                ExceptionHelper.HandleException(ex, typeof(Helper));
                responseMsg.HasError = true;
                responseMsg.ErrorMessage = ExceptionMessage;
            }
            return responseMsg;
        }


        /// <summary>
        /// UpdateWatchmenTable
        /// </summary>
        /// <param name="listWatchman"></param>
        /// <returns></returns>
        public static CrudResponseMessage UpdateWatchmenTable(List<Watchman> listWatchman)
        {
            CrudResponseMessage responseMsg = new CrudResponseMessage();
            try
            {
                IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
                responseMsg = client.UpdateWatchmenTable(listWatchman.ToArray());
            }
            catch (Exception ex)
            {
                ExceptionHelper.HandleException(ex, typeof(Helper));
                responseMsg.HasError = true;
            }
            return responseMsg;
        }

        public static void ShowMessage(string message)
        {
            ModernDialog dialog = new ModernDialog();
            dialog.Content = message;
            dialog.ShowDialog();
        }

        public static FundLogResponseMessage GetMinusFunds(string userID, string fundDate)
        {
            FundLogResponseMessage responseMsg = new FundLogResponseMessage();
            try
            {
                IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
                responseMsg = client.GetMinusFundLogs(userID, fundDate);
            }
            catch (Exception ex)
            {
                ExceptionHelper.HandleException(ex, typeof(Helper));
                responseMsg.HasError = true;
            }
            return responseMsg;
        }

        public static FundLogResponseMessage GetPlusFunds(string userID, string fundDate)
        {
            FundLogResponseMessage responseMsg = new FundLogResponseMessage();
            try
            {
                IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
                responseMsg = client.GetPlusFundLogs(userID, fundDate);
            }
            catch (Exception ex)
            {
                ExceptionHelper.HandleException(ex, typeof(Helper));
                responseMsg.HasError = true;
            }
            return responseMsg;
        }

        public static FundLogResponseMessage GetAllFunds(string userID, string fundDate)
        {
            IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
            return client.GetAllFundLogs(userID, fundDate);
        }

        public static CrudResponseMessage AddNewFundLog(string userID, string fundAmount, int isFundOut, string fundDate, string fundTime)
        {
            IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
            CrudResponseMessage response = client.InsertFundLog(userID, fundAmount, isFundOut, fundDate, fundTime);
            return response;
        }

        public static CrudResponseMessage SaveUserPhoto(string fileName, byte[] stream)
        {
            CrudResponseMessage responseMsg = new CrudResponseMessage();
            try
            {
                IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
                responseMsg = client.SaveUsersPhoto(fileName, stream);
            }
            catch (Exception ex)
            {
                ExceptionHelper.HandleException(ex, typeof(Helper));
                responseMsg.HasError = true;
            }
            return responseMsg;
        }
    }
}
